/***
 * 二次确认弹窗
 * use example：
 *  SecondConfirm({
      buttons: [
        {
          text: '删除',
          cb: async () => {
            const res = await DeleteUser(id);
            if (res.status === 200) {
              Toast.success('删除成功');
            }
          }
        }, {
          text: '取消'
        }
      ],
      message: `确认删除${name}的信息？`
    })
 * 
 */
import { ActionSheet } from 'antd-mobile-v2';
import styles from './index.module.less';

interface Button {
  text: string;
  cb?: () => any;
}

interface Props {
  buttons: Button[];
  message: string;
}

export const SecondConfirm = (props: Props) => {
  const { buttons, message } = props;
  ActionSheet.showActionSheetWithOptions(
    {
      options: buttons.map((item: Button) => item.text),
      cancelButtonIndex: buttons.length - 1, // 取消按钮索引，最后一项
      destructiveButtonIndex: buttons.length - 2, // 删除索引，倒数第二项
      className: styles.actionConfirmModal,
      message
    },
    async (buttonIndex) => {
      if (buttonIndex === buttons.length - 2) {
        const cb = buttons[buttons.length - 2].cb;
        cb && cb();
      } else {
        console.log('cancel');
      }
    }
  );
};